<?php
namespace Auth\Model;

use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Sql;
use Zend\Stdlib\Hydrator\ClassMethods;
class ListMapper 
{
    protected $tableName = 'list';
    protected $sql;
    protected $dbAdapter;
    
    public function __construct(Adapter $dbAdapter) 
    {
        $this->dbAdapter = $dbAdapter;
        $this->sql = new Sql($dbAdapter);
        $this->sql->setTable($this->tableName);
    }
    
    public function addMember(ListEntity $list) 
    {
        $hydrator = new ClassMethods();
        $data = $hydrator->extract($list);
        if(empty($data))
            return null;
        $action = $this->sql->insert();
        $action->values($data);
        $statement = $this->sql->prepareStatementForSqlObject($action);
        $result = $statement->execute();
        if(!$list->getId())
            $list->setId($result->getGeneratedValue());
        return $list;
    }
    
    public function _isExist($email)
    {
        $select = $this->sql->select();
        $select->where(array('email' => $email));
        $statement = $this->sql->prepareStatementForSqlObject($select);
        $result = $statement->execute()->current();
        if($result)
            return 1;
        return null;
    }
}